package bud.web.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import bud.vo.ShopDTO;
import bud.web.shop.service.IShopService;
import bud.web.shop.service.IShopService.ShopResult;


@Controller
public class ShopController {
	@Inject
	IShopService service;
	
	public IShopService getService() {
		return service;
	}


	public void setService(IShopService service) {
		this.service = service;
	}


	@RequestMapping("shopList")	//value 생략 가능
	public String shopList(Model model) {
		List<ShopDTO> shopList = service.getShopList();
		model.addAttribute("shopList", shopList);
		String viewName = "shop/shopList";	//중복되는 부분은 xml에서 설정
		return viewName;
	}
	
	@RequestMapping("shopView")
	public String shopView(@RequestParam(value="sh_no")int sh_no
			, Model model, HttpServletResponse response) throws IOException {	
		ShopDTO shop = service.getShop(sh_no);
		if(shop == null) {
			response.sendError(HttpServletResponse.SC_BAD_REQUEST,
					"조회하고 싶은 회원이 누구지?");
			return null;
		}
		model.addAttribute("shop", shop);
		String viewName = "shop/shopView";
		return viewName;
	}
	
	@RequestMapping("shopForm")
	public String shopForm() {
		return "shop/shopForm";
	}
	
	
	@RequestMapping("shopInsert")
	public String shopInsert(@ModelAttribute("shop") ShopDTO shop,
			Errors error,
			Model model) throws UnsupportedEncodingException {
		if(error.hasErrors()) {
			return "shop/shopForm";
		}
			
		ShopResult result = service.createShop(shop);
		String msg = null;
		String viewName = null;
		if(result == ShopResult.OK) {
			msg = URLEncoder.encode("정상적으로 가입이 되었습니당^.~","UTF-8");
			model.addAttribute("msg", msg);
			viewName = "redirect:/shopList.do";
		} else { //Faild
			msg = "서버측 오류 발생! 잠시 후 다시 등록해주세요^^";
			model.addAttribute("msg", msg);
			viewName = "shop/shopForm";
		}
		return viewName;
	}
	
	@RequestMapping(value="shopUpdate",method=RequestMethod.GET)
	public ModelAndView memberUpdate(@ModelAttribute("shop") ShopDTO shop,
			BindingResult error,
			ModelMap model, HttpServletResponse response) throws IOException {
		String viewName = "";
		if(error.hasErrors()) {
			viewName = "shop/shopView";
		} else {
			ShopResult result = service.modifyShop(shop);
			String msg = "";
			
			if(result == ShopResult.OK) {
				msg=URLEncoder.encode("정상 수정 완료","UTF-8");
				model.addAttribute("msg", msg);
				viewName = "redirect:/shopView.do?sh_no="+shop.getSh_no()+"&msg="+msg;
				
			} else if(result == ShopResult.Failed) {
				msg="알수없는 이유로 수정에 실패ㅜㅜ ";
				model.addAttribute("msg", msg);
				viewName="shop/shopView";
				
			} else if(result == ShopResult.NOTEXIST) {
				response.sendError(HttpServletResponse.SC_BAD_REQUEST,"정상적인 루트를 타지 않았네요");
				return null;
			} else if(result == ShopResult.INVALIDPASSWORD) {
				msg="비밀번호 오류ㅜㅜ";
				model.addAttribute("msg", msg);
				viewName="shop/shopView";
			}
		}
		ModelAndView mav = new ModelAndView();
		mav.addAllObjects(model);
		mav.setViewName(viewName);
		return mav;
	}
	
	@RequestMapping("shopDelete")
	public String shopDelete(@ModelAttribute("shop")ShopDTO shop,
			HttpServletResponse response, Model model) throws IOException {
//		member.setMem_id(mem_id);
//		member.setMem_pass(mem_pass);
		ShopResult result = service.removeShop(shop);
		String viewName="";
		String msg = "";
		
		if(result == ShopResult.OK) {
			msg=URLEncoder.encode("정상 탈퇴 완료","UTF-8");
			model.addAttribute("msg", msg);
			viewName = "shop/shopList";
		} else if(result == ShopResult.Failed) {
			msg=URLEncoder.encode("알수없는 이유로 탈퇴에 실패ㅜㅜ","UTF-8");
			model.addAttribute("msg", msg);
			viewName = "/shop/shopView";
		} else if(result == ShopResult.NOTEXIST) {
			response.sendError(HttpServletResponse.SC_BAD_REQUEST,
					"정상적인 루트를 타지 않았네요");	//400번대 에러 발생
			return null;
		} else if(result == ShopResult.INVALIDPASSWORD) {
			msg="비밀번호 오류ㅜㅜ";
			model.addAttribute("msg", msg);
			viewName="shop/shopView";
		}
		return viewName;
	}
}


